package socketinterface;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewSite;

import socketinterface.server.RequestSocket;

import com.vast.ui.ledpanel.Activator;
import com.vast.ui.ledpanel.views.ViewPart;

public class CommunicationManager {
	public  final ViewPart         view;
	public  Logger                 logger;
	private Action                 startAction;
	private Action                 stopAction;
	private int                    serverPort   = 20000;
	private ServerSocket           serverSocket = null;
	private RequestSocket          requestSocket;
	private BlockingSocketReceiver receiver     = null;
	private boolean                isServerEstablished = false;
	
	public CommunicationManager(ViewPart view, int serverPort) {
		this.view       = view;
		this.logger     = new ConsoleLogger();
		this.serverPort = serverPort;
		
		
		startAction = new Action() {
			public void run() {
				openConnection();
			}
		};
		startAction.setEnabled(true);
		startAction.setToolTipText("Start SocketServer");
		startAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("START_ENABLED"));
		
		stopAction = new Action() {
			public void run() {
				closeConnection();
			}
		};
		stopAction.setToolTipText("Stop SocketServer");
		stopAction.setEnabled(false);
		stopAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("STOP_ENABLED"));
		
		IViewSite viewSite = this.view.getViewSite();
		IActionBars actionBars = viewSite.getActionBars();
		IToolBarManager toolbar = actionBars.getToolBarManager();
		toolbar.add(startAction);
		toolbar.add(stopAction);
				
	}
	
	public void openConnection(){
		if(!isEstablished()){
			logger.writeEntry("openConnection()");
			startAction.setEnabled(false);
			stopAction.setEnabled(true);
			
			startServer();
		}
	}
	
	public void closeConnection(){
			setEstablished(false);
			startAction.setEnabled(true);
			stopAction.setEnabled(false);
	}
	
	public void setPortNumber(int portNumber) {
		this.serverPort = portNumber;
	}
	
	public synchronized void setEstablished(boolean value) {
		isServerEstablished = value;
	}
	public synchronized boolean isEstablished() {
		// TODO Auto-generated method stub
		return isServerEstablished;
	}
	
	public void startServer() {
		// TODO Auto-generated method stub
		requestSocket   = new RequestSocket();
		receiver = new BlockingSocketReceiver(view, requestSocket, logger);
		new Thread(receiver).start();
		new Thread(new Runnable() {
			public void run() {
				try {
					serverSocket = new ServerSocket(serverPort);
					setEstablished(true);
					logger.writeEntry("Waiting client access. Server port: " + serverPort);
					requestSocket.openClientSocket(serverSocket);				
					logger.writeEntry("Finish receiver.run()");
					serverSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}
	
}
